// Author: Puyan Lotfi
// Date: Written in June 2009
// Description: UI Code for FooScope

#include <QFileDialog>
#include <QFileInfo>
#include <QTableWidgetItem>
#include <Qsci/qsciscintilla.h>
#include <Qsci/qscilexer.h>

#include "ui_mainwindow.h"
#include "mainwindow.h"
#include "ctagsreader.h"
#include "cpphighlighter.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_currentDirectory = QApplication::applicationDirPath();

    ui->textEdit->setMarginLineNumbers(1, true);
    ui->textEdit->setFolding(QsciScintilla::PlainFoldStyle, 2);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionOpen_triggered()
{
    QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), ".", tr("*"));
    QsciLexer* lexer = m_lang.getLexer(filename);

    ui->textEdit->setLexer(lexer);

    if(!QFile::exists(filename)) return ;
    QString fileText = m_helper.loadFileText(filename);
    ui->textEdit->setText(fileText);

    int lineCount = ui->textEdit->lines();
    int lineCountOrderOfMagnitude = 1;

    for(int countStart=lineCount; countStart > 1; )
    {
        lineCountOrderOfMagnitude++;
        countStart /= 10;
    }


    ui->textEdit->setMarginWidth(1, 10 * lineCountOrderOfMagnitude);

}

void MainWindow::on_actionSave_triggered()
{
    m_helper.saveFileText(ui->textEdit->text());
}

void MainWindow::on_actionSave_As_triggered()
{
    QString foo = m_helper.getCurrentFileExtension();
    QString filename = QFileDialog::getSaveFileName(this, tr("Save File"), ".", tr("*"));
    m_helper.saveFileText(ui->textEdit->text(), filename);
}

void MainWindow::on_actionOpen_Tag_File_triggered()
{
    QString filename = QFileDialog::getOpenFileName(this, tr("Open Tag File"), ".", tr("*"));

    if(!QFile::exists(filename)) return; // TODO: Throw an Exception, or pop a dialog box up or something.

    ui->treeWidget->clear();
    m_tagsReader.clearTags();
    m_tagsReader.loadTagsFromFile(filename);
    QFileInfo fileInfo(filename);
    m_currentDirectory = fileInfo.absoluteDir().absolutePath(); // XXX
}

void MainWindow::on_lineEdit_returnPressed()
{
    QString query = ui->lineEdit->text();
    ui->lineEdit->clear();
    ui->treeWidget->clear();

    if(query.length() > 0)
    {
        std::vector<Tag> tags = m_tagsReader.getTagsByQuery(query);

        for(size_t i=0; i<tags.size();i++)
        {
            Tag tag = tags[i];

            QTreeWidgetItem* item = new QTreeWidgetItem();
            item->setText(2, tag.m_code);
            item->setText(1, tag.m_filename);
            item->setText(0, tag.m_tagname);
            ui->treeWidget->addTopLevelItem(item);
        }
    }
}

void MainWindow::on_treeWidget_itemDoubleClicked(QTreeWidgetItem* item, int column)
{
    QString filename = item->text(1);
    QsciLexer* lexer = m_lang.getLexer(filename);

    ui->textEdit->setLexer(lexer);

    QString code = item->text(2);
    QFileInfo fileInfo(m_currentDirectory, filename);
    QString absoluteFilePath = fileInfo.absoluteFilePath();
    QString fileText = m_helper.loadFileText(absoluteFilePath);

    ui->textEdit->setText(fileText);

    bool found = ui->textEdit->findFirst(code, false, true, false, true, true, 0, 0, true);  //->find(code, QTextDocument::FindCaseSensitively);
    if(!found) return; // TODO: Popup a dialog
}

void MainWindow::on_actionExit_triggered()
{
    exit(1);
}

void MainWindow::on_actionFont_triggered()
{

}
